package com.github.binarylei.zkclient.watcher;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

/**
 * 监听节点变化
 * @author: leigang
 * @version: 2018-04-06
 */
public class zkClientWatcher2 {

    private static final String CONNECT_ADDR = "127.0.0.1:2181";
    private static final int SEESION_OUTTIME = 5 * 1000;
    
    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient(new ZkConnection(CONNECT_ADDR), SEESION_OUTTIME);
        zkClient.deleteRecursive("/super");
        zkClient.createPersistent("/super");

        // 对父节点添加子节点变化的事件
        zkClient.subscribeDataChanges("/super", new IZkDataListener() {
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("变量的节点为：" + dataPath + "，变更的内容为：" + data);
            }

            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println("删除的节点为：" + dataPath);
            }
        });

        zkClient.writeData("/super", "super");
        Thread.sleep(1000);
    }

}
